package com.kantboot.business.gc.repository;

import com.kantboot.business.gc.domain.dto.BusGcCollectSearchUserDTO;
import com.kantboot.business.gc.domain.entity.BusGcUserOpenGame;
import com.kantboot.business.gc.domain.vo.BusGcGameVO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface BusGcUserOpenGameRepository extends JpaRepository<BusGcUserOpenGame, Long> {

    List<BusGcUserOpenGame> findByUserId(Long userId);
    //
    List<BusGcUserOpenGame> findByUserIdAndGameId(Long selfId, Long gameId);

    @Query("""
            SELECT t FROM BusGcUserOpenGame t
            WHERE
            t.gameId = :gameId
            ORDER BY t.gmtModified ASC
            """)
    List<BusGcUserOpenGame> findAllByGameId(Long gameId);

    @Query("""
            SELECT DISTINCT a
            FROM BusGcGameVO a
            LEFT JOIN BusGcUserOpenGame b ON a.id = b.gameId
            WHERE
            (:#{#param.userId} IS NULL OR :#{#param.userId} = '' OR b.userId = :#{#param.userId})
            AND (:#{#param.zhName} IS NULL OR :#{#param.zhName} = '' OR a.zhName = :#{#param.zhName})
            AND (:#{#param.enName} IS NULL OR :#{#param.enName} = '' OR a.enName = :#{#param.enName})
            AND (:#{#param.zhDescription} IS NULL OR :#{#param.zhDescription} = '' OR a.zhDescription = :#{#param.zhDescription})
            AND (:#{#param.enDescription} IS NULL OR :#{#param.enDescription} = '' OR a.enDescription = :#{#param.enDescription})
            ORDER BY a.priority DESC
            """)
    Page<BusGcGameVO> getBodyDataByUserId(@Param("param") BusGcCollectSearchUserDTO param, Pageable pageable);
}
